﻿Shader "Hidden/BlitImg"
{
	CGINCLUDE
	#include "UnityCG.cginc"  
	#include "Packages/com.devil.gametoolkit/Shaders/CGINC/FullScreenTriangle.cginc"

	#define WEIGHT float3(0.147761, 0.118318, 0.0947416)

	struct appdata
	{
		float4 vertex : POSITION;
	};

	struct v2f
	{
		float4 pos : SV_POSITION;   //顶点位置  
		float2 uv  : TEXCOORD0;     //纹理坐标 
	};

	//shader中用到的参数  
	sampler2D _MainTex;
	#if defined(_SRP_URP)
		sampler2D_float _CameraDepthAttachment;
		#define _CameraDepthTexture _CameraDepthAttachment
	#else
		sampler2D_float _CameraDepthTexture;
	#endif
	sampler2D _Tex1;
	sampler2D _Tex2;
	sampler2D _Tex3;
	sampler2D _Tex4;
	//XX_TexelSize，XX纹理的像素相关大小width，height对应纹理的分辨率，x = 1/width, y = 1/height, z = width, w = height  
	fixed4 _Color; // clear color

	v2f vert_color(appdata v)
	{
		v2f o;
		// o.pos = TransformFullScreenTriangleVertexToClip(v.vertex.xy);
		// o.uv = TransformFullScreenTriangleVertexToUV(v.vertex.xy);
		INIT_FULL_SCREEN_TRIANGLE_VERTEX(v.vertex,o.uv,o.pos)
		return o;
	}

	fixed4 clear_color(v2f i) : SV_TARGET
	{
		return _Color;
	}

	fixed4 blit_color(v2f i) : SV_TARGET
	{
		fixed4 col = tex2D(_MainTex, i.uv);
		return col * _Color;
	}

	fixed4 blit(v2f i) : SV_TARGET
	{
		return tex2D(_MainTex, i.uv);
	}

	void blit1(v2f i, out fixed4 rt0 : SV_Target0, out fixed4 rt1 : SV_Target1)
	{
		rt0 = tex2D(_MainTex, i.uv);
		rt1 = tex2D(_Tex1, i.uv);
	}
	
	void blit2(v2f i, out fixed4 rt0 : SV_Target0, out fixed4 rt1 : SV_Target1, out fixed4 rt2 : SV_Target2)
	{
		rt0 = tex2D(_MainTex, i.uv);
		rt1 = tex2D(_Tex1, i.uv);
		rt2 = tex2D(_Tex2, i.uv);
	}

	void blit3(v2f i, out fixed4 rt0 : SV_Target0, out fixed4 rt1 : SV_Target1, out fixed4 rt2 : SV_Target2, out fixed4 rt3: SV_Target3)
	{
		rt0 = tex2D(_MainTex, i.uv);
		rt1 = tex2D(_Tex1, i.uv);
		rt2 = tex2D(_Tex2, i.uv);
		rt3 = tex2D(_Tex3, i.uv);
	}

	
	fixed4 stack(v2f i) : SV_TARGET
	{
		#if UNITY_UV_STARTS_AT_TOP
			i.uv.y = 1-i.uv.y;
		#endif
		return tex2D(_MainTex, i.uv);
	}

	float4 fragCopyDepth(v2f i): SV_TARGET
	{
		return tex2D(_CameraDepthTexture, i.uv);
	}

	ENDCG

	SubShader
	{

		//Stencil
		//{
			//	WriteMask [_StencilMask]
			//	Comp Always
			//	Pass [_StencilOp]
		//}

		// clear pass
		Pass
		{
			Name "Clear"
			ZTest Off
			ZWrite Off
			Cull Off
			Blend One Zero

			CGPROGRAM
			#pragma vertex vert_color
			#pragma fragment clear_color
			#pragma shader_feature_local _ _SRP_URP

			ENDCG
		}

		// blit
		Pass
		{
			Name "BlitImg"
			ZTest Off
			ZWrite Off
			Cull Off
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM
			#pragma vertex vert_color
			#pragma fragment blit_color
			#pragma shader_feature_local _ _SRP_URP

			ENDCG
		}

		// Stencil only
		Pass
		{
			Name "StencilOnly"
			ZTest Off
			ZWrite Off
			Cull Off
			Blend Zero One
			
			CGPROGRAM
			#pragma vertex vert_color
			#pragma fragment clear_color
			#pragma shader_feature_local _ _SRP_URP

			ENDCG
		}

		// copy
		Pass
		{
			Name "CpyImg"
			ZTest Off
			ZWrite Off
			Cull Off
			Blend One Zero

			CGPROGRAM
			#pragma vertex vert_color
			#pragma fragment blit
			#pragma shader_feature_local _ _SRP_URP

			ENDCG
		}

		Pass
		{
			Name "CpyImg1"
			ZTest Off
			ZWrite Off
			Cull Off
			Blend One Zero

			CGPROGRAM
			#pragma vertex vert_color
			#pragma fragment blit1
			#pragma shader_feature_local _ _SRP_URP

			ENDCG
		}

		Pass
		{
			Name "CpyImg2"
			ZTest Off
			ZWrite Off
			Cull Off
			Blend One Zero

			CGPROGRAM
			#pragma vertex vert_color
			#pragma fragment blit2
			#pragma shader_feature_local _ _SRP_URP

			ENDCG
		}
		
		Pass
		{
			Name "CpyImg3"
			ZTest Off
			ZWrite Off
			Cull Off
			Blend One Zero

			CGPROGRAM
			#pragma vertex vert_color
			#pragma fragment blit3
			#pragma shader_feature_local _ _SRP_URP

			ENDCG
		}

		Pass
		{
			Name "CpyDepth"
			ZTest Off
			ZWrite Off
			Cull Off
			Blend One Zero

			CGPROGRAM
			#pragma vertex vert_color
			#pragma fragment fragCopyDepth
			#pragma shader_feature_local _ _SRP_URP

			ENDCG
		}

		// stack
		Pass
		{
			Name "StackImg"
			ZTest Off
			ZWrite Off
			Cull Off
			Blend SrcAlpha OneMinusSrcAlpha
			ColorMask RGB

			CGPROGRAM
			#pragma vertex vert_color
			#pragma fragment stack
			#pragma shader_feature_local _ _SRP_URP

			ENDCG
		}
	}
	//Fallback "Diffuse"
}
